
package com.sskj.jkzx.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sskj.jkzx.domain.entity.EmpQueryParam;
import com.sskj.jkzx.domain.entity.EngQueryParam;
import com.sskj.jkzx.domain.entity.OrgQueryParam;
import com.sskj.jkzx.domain.entity.basic.KeyValue;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "**/sskj/search")
@Api(value = "搜索服务")
public class SearchServiceImpl {

	protected final Log logger = LogFactory.getLog(SearchServiceImpl.class);

	@Value("${sskj.jdServer}")
	private String jdServer;

	@Value("${sskj.tsServer}")
	private String tsServer;

	@Value("${sskj.cmisServer}")
	private String cmisServer;

	/***
	 * 查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/jsxdata", method = RequestMethod.GET)
	@ApiOperation(value = "信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "关键字", paramType = "form", dataType = "string") })
	public List<KeyValue> queryEngSgxkInfo(String name) {

		List<KeyValue> results = new ArrayList<KeyValue>();
		if (name != null && name.length() >= 2) {

			try {
				String str1 = HttpHelp.sendPost(jdServer + "/seam/resource/v1/jkzx/eng/search",
						JSON.toJSONString(new EngQueryParam(name)));

				if (str1 != null) {
					results.addAll(JSON.parseArray(str1, KeyValue.class));
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}

			try {
				String str2 = HttpHelp.sendPost(cmisServer + "/seam/resource/v1/jkzx/emp/search",
						JSON.toJSONString(new EmpQueryParam(name)));
				if (str2 != null) {
					results.addAll(JSON.parseArray(str2, KeyValue.class));
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}

			try {
				String str3 = HttpHelp.sendPost(cmisServer + "/seam/resource/v1/jkzx/org/search",
						JSON.toJSONString(new OrgQueryParam(name)));
				if (str3 != null) {
					results.addAll(JSON.parseArray(str3, KeyValue.class));
				}
			} catch (Exception e) {
				logger.info(e.getMessage());
			}

		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}
}
